package com.itany.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class OssClientUtil {
    // 阿里域名
    public static final String ALI_DOMAIN= "https://life-bucket-hao.oss-cn-nanjing.aliyuncs.com";
    public static Map<String,Object> uploadImage(String folderName,MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return null; // 如果文件为空，则返回提示信息"未上传文件"
        }
        String originalFilename = file.getOriginalFilename(); // 获取原始文件名
        String ext='.' + originalFilename.split("\\.")[1]; // 获取文件扩展名
        String uuid = UUID.randomUUID().toString().replace("-", ""); // 生成随机UUID
        String newFileName = folderName + uuid + ext; // 新文件名
        String fileName = uuid + ext;
        // 地域节点
        String endpoint = "https://oss-cn-nanjing.aliyuncs.com";
        String accessKeyID = "LTAI5tAs6gT7qfkYyG1Wcdwx";
        String accessKeySecret = "w1zuiUvlGzAjN9sXT74quqwzeOs98m";
        // OSS客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyID, accessKeySecret);
        ossClient.putObject(
                "life-bucket-hao", // 仓库名
                newFileName,  // 文件名
                file.getInputStream());
        ossClient.shutdown();
        Map<String,Object> map = new HashMap<>();
        map.put("path",ALI_DOMAIN);
        map.put("filename",fileName);
        return map;
    }

    public static void deleteImage(String objectKey) {
        String endpoint = "https://oss-cn-nanjing.aliyuncs.com";
        String accessKeyID = "LTAI5tAs6gT7qfkYyG1Wcdwx";
        String accessKeySecret = "w1zuiUvlGzAjN9sXT74quqwzeOs98m";
        String bucketName = "life-bucket-hao";

        // OSS客户端对象
        OSS ossClient = null;
        try {
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyID, accessKeySecret);
            ossClient.deleteObject(bucketName, objectKey);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    public static MultipartFile convert(String imageUrl) throws IOException {
        // 创建URL对象
        URL url = new URL(imageUrl);

        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        // 获取输入流
        InputStream inputStream = connection.getInputStream();

        // 将输入流转换为字节数组
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        byte[] imageBytes = outputStream.toByteArray();

        // 关闭流
        inputStream.close();
        outputStream.close();

        // 创建MultipartFile对象
        return new MockMultipartFile("file", "image.jpg", "image/jpeg", imageBytes);
    }
}
